package com.moonspring.contronller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.moonspring.dao.UserDao;
import com.moonspring.model.UserModel;

@WebServlet("/user/login")
public class UserLoginController extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username  = req.getParameter("username");
		String pwd = req.getParameter("pwd");
		UserModel user = new UserModel();
		user.setPwd(pwd);
		user.setUserName(username);
		UserDao ud = new UserDao();
		user = ud.userLogin(user);
		String r = "{\"stat\":-1,\"msg\":\"peo err\"}";
		switch (user.getLginStat()) {
		case 1:
			HttpSession sse = req.getSession();
			sse.setAttribute("user", user);
			r = "{\"stat\":1,\"msg\":\"ok\"}";
			break;
			
		case -1:
			r = "{\"stat\":-1,\"msg\":\"peo err\"}";
			break;
		case -2:
			r = "{\"stat\":-2,\"msg\":\"db err\"}";
			break;

		default:
			break;
		}
		PrintWriter w = resp.getWriter();
		w.print(r);
		w.close();
	}
	
}
